package com.xty.xtyle.mix;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.xty.xtyle.R;
import com.xty.xtyle.db.cloth.Cloth;
import com.xty.xtyle.db.cloth.ClothDao;
import com.xty.xtyle.db.mix.MixCloth;
import com.xty.xtyle.db.mix.MixDao;
import com.xty.xtyle.db.mix.MixType;
import com.xty.xtyle.db.mix.MixTypeDao;
import com.xty.xtyle.utils.CommonUtil;

import java.sql.Time;

public class MixDetialActivity extends AppCompatActivity {

    private static final int leftUp = 1;
    private static final int leftDown = 2;
    private static final int rightUp = 3;
    private static final int rightCenter = 4;
    private static final int rightDown = 5;

    ImageView image_up, image_down, pie_up, pie_middle, pie_down;
    Cloth leftUpCloth, leftDownCloth, rightUpCloth, rightMidCloth, rightDownCloth;
    MixDao mixDao = new MixDao();
    long mixTime;
    EditText edit_type;
    MixCloth mixCloth;

    public static void startActivity(Context context, long id) {
        Intent intent = new Intent(context, MixDetialActivity.class);
        intent.putExtra("id", String.valueOf(id));
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_mix);
        initBar();

        loadFromDB();
    }

    /**
     * 显示搭配内容,如果id大于0,加载对应衣服的信息:包含图片和其他的
     */
    void loadFromDB() {
        long id = Long.parseLong(getIntent().getStringExtra("id"));
        mixCloth = mixDao.queryById(id);
        if (null == mixCloth) return;
        if (mixCloth.getUpId() > 0)
            decode(1, mixCloth.getUpId());
        if (mixCloth.getDownId() > 0)
            decode(2, mixCloth.getDownId());
        if (mixCloth.getPieUpId() > 0)
            decode(3, mixCloth.getPieUpId());
        if (mixCloth.getPieCenterId() > 0)
            decode(4, mixCloth.getPieCenterId());
        if (mixCloth.getPieDownId() > 0)
            decode(5, mixCloth.getPieDownId());
        edit_type.setText(mixCloth.getType());
    }

    /**
     * 5个图片的搭配,对应图片去选择衣服衣架中去选择
     */
    void initBar() {
        findViewById(R.id.left_icon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        TextView titleView = findViewById(R.id.center_icon);
        titleView.setText("添加搭配");
        findViewById(R.id.right_icon).setVisibility(View.INVISIBLE);

        image_up = findViewById(R.id.image_up);
        image_down = findViewById(R.id.image_down);
        pie_up = findViewById(R.id.pie_up);
        pie_middle = findViewById(R.id.pie_middle);
        pie_down = findViewById(R.id.pie_down);
        edit_type = findViewById(R.id.edit_type);
        //左边的上衣
        findViewById(R.id.image_up).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MixPicActivity.startActivity((Activity) view.getContext(), leftUp);
            }
        });
        //左边的下裤子
        findViewById(R.id.image_down).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MixPicActivity.startActivity((Activity) view.getContext(), leftDown);
            }
        });
        //衣服配件,可以理解为头饰
        findViewById(R.id.pie_up).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MixPicActivity.startActivity((Activity) view.getContext(), rightUp);
            }
        });
        //腰部配件,皮带
        findViewById(R.id.pie_middle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MixPicActivity.startActivity((Activity) view.getContext(), rightCenter);
            }
        });
        //底部配件,鞋子之类
        findViewById(R.id.pie_down).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MixPicActivity.startActivity((Activity) view.getContext(), rightDown);
            }
        });

        //添加到搭配中
        findViewById(R.id.mix_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                update();
            }
        });
    }

    //从衣架挑选回来的内容,通过position来确定是5张图片的内容,id为衣服的ID
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == MixPicActivity.PIC_STATE_BACK) {
            int position = data.getIntExtra("position", 0);
            int clothId = data.getIntExtra("id", -1);
            if (position < 1 || clothId < 0) return;
            decode(position, clothId);
        }
    }

    /**
     *
     * @param position 5张图片的位置
     * @param clothId 衣服的id
     */
    void decode(int position, int clothId) {
        ClothDao clothDao = new ClothDao();
        switch (position) {
            case 1:
                leftUpCloth = clothDao.findCloth(clothId);
                Glide.with(this).load(leftUpCloth.getImage()).into(image_up);
                break;
            case 2:
                leftDownCloth = clothDao.findCloth(clothId);
                Glide.with(this).load(leftDownCloth.getImage()).into(image_down);
                break;
            case 3:
                rightUpCloth = clothDao.findCloth(clothId);
                Glide.with(this).load(rightUpCloth.getImage()).into(pie_up);
                break;
            case 4:
                rightMidCloth = clothDao.findCloth(clothId);
                Glide.with(this).load(rightMidCloth.getImage()).into(pie_middle);
                break;
            case 5:
                rightDownCloth = clothDao.findCloth(clothId);
                Glide.with(this).load(rightDownCloth.getImage()).into(pie_down);
                break;
        }
    }

    /**
     * 如果挑选了一件也可以认为是搭配
     */
    public void update() {
        boolean load = false;
        if (null != leftUpCloth) {
            load = true;
            mixCloth.setUpId(leftUpCloth.getId());
        }
        if (null != leftDownCloth) {
            load = true;
            mixCloth.setDownId(leftDownCloth.getId());
        }
        if (null != rightUpCloth) {
            load = true;
            mixCloth.setPieUpId(rightUpCloth.getId());
        }
        if (null != rightMidCloth) {
            load = true;
            mixCloth.setPieCenterId(rightMidCloth.getId());
        }
        if (null != rightDownCloth) {
            load = true;
            mixCloth.setPieDownId(rightDownCloth.getId());
        }

        String type = edit_type.getText().toString();
        checkType(type);

        if (mixTime < 1) {
            showDialog();
            return;
        }

        if (load) {
            mixDao.update(mixCloth);
            CommonUtil.showToast("混搭添加成功");
        }
    }

    //时间选择对话框,当前要存储的搭配时间
    TimePickerDialog pickTimeDialog;
    private void showDialog() {
        pickTimeDialog = new TimePickerDialog();
        pickTimeDialog.show(getSupportFragmentManager(), "pick");
        pickTimeDialog.setCallBack(new TimePickerDialog.CallBack() {
            @Override
            public void time(long time) {
                Log.d("FlutterPush", "back  imageFile:"+time);
                if (time > 1) {
                    mixTime = time;
                }
            }
        });
    }

    /**
     * 如果类型没有，则添加
     * @param type
     */
    void checkType(String type) {
        if (!TextUtils.isEmpty(type)) {
            MixTypeDao clothTypesDao = new MixTypeDao();
            MixType types = clothTypesDao.findMixType(type);
            if (null == types) {
                MixType clothTypes = new MixType(type);
                clothTypes.save();
            }
        }
    }
}
